<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   在Python中使用线性回归预测数据  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/13355.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466460660: Accept with keywords: (title(0.714285714286):社区,预测,Python,回归,数盟,线性,数据, topn(0.4):社区,数盟,行业资讯,数据挖掘,价格,函数,文件,缺失值,Python,模型,职业规划,房子,电视节目,基础架构,文章,数据,闪电侠,预测,python,步骤,回归,编程,程序包,观众,拟合,线性,代码,程序,课程,节目).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    在Python中使用线性回归预测数据
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    在Python中使用线性回归预测数据 | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,65] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/13355.html">
          在Python中使用线性回归预测数据
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          6,765 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech" rel="category tag">
          文章
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <p>
        本文中，我们将进行大量的编程——但在这之前，我们先介绍一下我们今天要解决的实例问题。
       </p>
       <h2>
        1) 预测房子价格
       </h2>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/03/6941baebgw1eqeoyz4k5lj218g0l5qa7.jpg"/>
       </p>
       <p>
        我们想预测特定房子的价值，预测依据是房屋面积。
       </p>
       <h2>
        2) 预测下周哪个电视节目会有更多的观众
       </h2>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/03/6941baebgw1eqeoyybd5tj20hr0a0dgx1.jpg"/>
       </p>
       <p>
        闪电侠和绿箭侠是我最喜欢的电视节目。我想看看下周哪个节目会有更多的观众。
       </p>
       <h2>
        3) 替换数据集中的缺失值
       </h2>
       <p>
        我们经常要和带有缺失值的数据集打交道。这部分没有实战例子，不过我会教你怎么去用线性回归替换这些值。
       </p>
       <h2>
        所以，让我们投入编程吧（马上）
       </h2>
       <p>
        在动手之前，去把我以前的文章(
        <a href="http://dataconomy.com/python-packages-for-data-mining">
         Python Packages for Data Mining
        </a>
        )中的程序包安装了是个好主意。
       </p>
       <h2>
        1) 预测房子价格
       </h2>
       <p>
        我们有下面的数据集：
       </p>
       <table>
        <thead>
         <tr>
          <th>
           输入编号
          </th>
          <th>
           平方英尺
          </th>
          <th>
           价格
          </th>
         </tr>
        </thead>
        <tbody>
         <tr>
          <td>
           1
          </td>
          <td>
           150
          </td>
          <td>
           6450
          </td>
         </tr>
         <tr>
          <td>
           2
          </td>
          <td>
           200
          </td>
          <td>
           7450
          </td>
         </tr>
         <tr>
          <td>
           3
          </td>
          <td>
           250
          </td>
          <td>
           8450
          </td>
         </tr>
         <tr>
          <td>
           4
          </td>
          <td>
           300
          </td>
          <td>
           9450
          </td>
         </tr>
         <tr>
          <td>
           5
          </td>
          <td>
           350
          </td>
          <td>
           11450
          </td>
         </tr>
         <tr>
          <td>
           6
          </td>
          <td>
           400
          </td>
          <td>
           15450
          </td>
         </tr>
         <tr>
          <td>
           7
          </td>
          <td>
           600
          </td>
          <td>
           18450
          </td>
         </tr>
        </tbody>
       </table>
       <p>
        <strong>
         步骤：
        </strong>
       </p>
       <p>
        在
        <a href="http://dataaspirant.com/2014/10/02/linear-regression/">
         线性回归
        </a>
        中，我们都知道必须在数据中找出一种线性关系，以使我们可以得到θ0和θ1。 我们的假设方程式如下所示：
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/03/6941baebgw1eqeoyxxtmkj20at01q3yf.jpg"/>
       </p>
       <p>
        其中： hθ(x)是关于特定平方英尺的价格值（我们要预测的值），（意思是价格是平方英尺的线性函数）； θ0是一个常数； θ1是回归系数。
       </p>
       <p>
        那么现在开始编程：
       </p>
       <p>
        <strong>
         步骤1
        </strong>
       </p>
       <p>
        打开你最喜爱的文本编辑器，并命名为predict_house_price.py。 我们在我们的程序中要用到下面的包，所以把下面代码复制到predict_house_price.py文件中去。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_598734">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                # Required Packages
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                matplotlib.pyplot as plt
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                numpy as np
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                pandas as pd
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python keyword">
                from
               </code>
               <code class="python plain">
                sklearn
               </code>
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                datasets, linear_model
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        运行一下你的代码。如果你的程序没错，那步骤1基本做完了。如果你遇到了某些错误，这意味着你丢失了一些包，所以回头去看看
        <a href="http://dataconomy.com/python-packages-for-data-mining/">
         包的页面
        </a>
        。 安装博客文章中所有的包，再次运行你的代码。这次希望你不会遇到任何问题。
       </p>
       <p>
        现在你的程序没错了，我们继续……
       </p>
       <p>
        <strong>
         步骤2
        </strong>
        <br/>
        我把数据存储成一个.csv文件，名字为input_data.csv 所以让我们写一个函数把数据转换为X值（平方英尺）、Y值（价格）
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_77613">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                # Function to get data
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                get_data(file_name):
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                data
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                pd.read_csv(file_name)
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                X_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                Y_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                for
               </code>
               <code class="python plain">
                single_square_feet ,single_price_value
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python functions">
                zip
               </code>
               <code class="python plain">
                (data[
               </code>
               <code class="python string">
                'square_feet'
               </code>
               <code class="python plain">
                ],data[
               </code>
               <code class="python string">
                'price'
               </code>
               <code class="python plain">
                ]):
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                X_parameter.append([
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (single_square_feet)])
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                Y_parameter.append(
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (single_price_value))
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                X_parameter,Y_parameter
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        第3行：将.csv数据读入Pandas数据帧。
       </p>
       <p>
        第6-9行：把Pandas数据帧转换为X_parameter和Y_parameter数据，并返回他们。
       </p>
       <p>
        所以，让我们把X_parameter和Y_parameter打印出来：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate text" id="highlighter_498295">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="text plain">
                [[150.0], [200.0], [250.0], [300.0], [350.0], [400.0], [600.0]]
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="text plain">
                [6450.0, 7450.0, 8450.0, 9450.0, 11450.0, 15450.0, 18450.0]
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="text plain">
                [Finished in 0.7s]
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        脚本输出：
        <code>
         [[150.0], [200.0], [250.0], [300.0], [350.0], [400.0], [600.0]] [6450.0, 7450.0, 8450.0, 9450.0, 11450.0, 15450.0, 18450.0] [Finished in 0.7s]
        </code>
       </p>
       <p>
        <strong>
         步骤3
        </strong>
       </p>
       <p>
        现在让我们把X_parameter和Y_parameter拟合为线性回归模型。我们要写一个函数，输入为X_parameters、Y_parameter和你要预测的平方英尺值，返回θ0、θ1和预测出的价格值。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_973649">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                # Function for Fitting our data to Linear model
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                linear_model_main(X_parameters,Y_parameters,predict_value):
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                # Create linear regression object
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                linear_model.LinearRegression()
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr.fit(X_parameters, Y_parameters)
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predict_outcome
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                regr.predict(predict_value)
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predictions
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                {}
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predictions[
               </code>
               <code class="python string">
                'intercept'
               </code>
               <code class="python plain">
                ]
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                regr.intercept_
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predictions[
               </code>
               <code class="python string">
                'coefficient'
               </code>
               <code class="python plain">
                ]
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                regr.coef_
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predictions[
               </code>
               <code class="python string">
                'predicted_value'
               </code>
               <code class="python plain">
                ]
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                predict_outcome
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                predictions
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        第5-6行：首先，创建一个线性模型，用我们的X_parameters和Y_parameter训练它。
       </p>
       <p>
        第8-12行：我们创建一个名称为predictions的字典，存着θ0、θ1和预测值，并返回predictions字典为输出。
       </p>
       <p>
        所以让我们调用一下我们的函数，要预测的平方英尺值为700。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_790436">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                X,Y
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                get_data(
               </code>
               <code class="python string">
                'input_data.csv'
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                predictvalue
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                700
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python plain">
                result
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                linear_model_main(X,Y,predictvalue)
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                print
               </code>
               <code class="python string">
                "Intercept value "
               </code>
               <code class="python plain">
                , result[
               </code>
               <code class="python string">
                'intercept'
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                "coefficient"
               </code>
               <code class="python plain">
                , result[
               </code>
               <code class="python string">
                'coefficient'
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                "Predicted value: "
               </code>
               <code class="python plain">
                ,result[
               </code>
               <code class="python string">
                'predicted_value'
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        脚本输出：
        <code>
         Intercept value 1771.80851064 coefficient [ 28.77659574] Predicted value: [ 21915.42553191] [Finished in 0.7s]
        </code>
       </p>
       <p>
        这里，Intercept value（截距值）就是θ0的值，coefficient value（系数）就是θ1的值。 我们得到预测的价格值为21915.4255——意味着我们已经把预测房子价格的工作做完了！
       </p>
       <p>
        为了验证，我们需要看看我们的数据怎么拟合线性回归。所以我们需要写一个函数，输入为X_parameters和Y_parameters，显示出数据拟合的直线。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_875866">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                # Function to show the resutls of linear fit model
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                show_linear_line(X_parameters,Y_parameters):
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                # Create linear regression object
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                linear_model.LinearRegression()
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr.fit(X_parameters, Y_parameters)
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                plt.scatter(X_parameters,Y_parameters,color
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'blue'
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                plt.plot(X_parameters,regr.predict(X_parameters),color
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'red'
               </code>
               <code class="python plain">
                ,linewidth
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                4
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                plt.xticks(())
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                plt.yticks(())
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                plt.show()
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        那么调用一下show_linear_line函数吧：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_889876">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_linear_line(X,Y)
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        脚本输出：
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/03/6941baebgw1eqeoyxkusaj20h80en755.jpg"/>
       </p>
       <p>
        2)预测下周哪个电视节目会有更多的观众
       </p>
       <p>
        <img src="http://dataunion.org/wp-content/uploads/2015/03/6941baebgw1eqeoyx1n4cj20h808wwfx.jpg"/>
       </p>
       <p>
        闪电侠是一部由剧作家/制片人Greg Berlanti、Andrew Kreisberg和Geoff Johns创作，由CW电视台播放的美国电视连续剧。它基于DC漫画角色闪电侠（Barry Allen），一个具有超人速度移动能力的装扮奇特的打击犯罪的超级英雄，这个角色是由Robert Kanigher、John Broome和Carmine Infantino创作。它是绿箭侠的衍生作品，存在于同一世界。该剧集的试播篇由Berlanti、Kreisberg和Johns写作，David Nutter执导。该剧集于2014年10月7日在北美首映，成为CW电视台收视率最高的电视节目。
       </p>
       <p>
        绿箭侠是一部由剧作家/制片人 Greg Berlanti、Marc Guggenheim和Andrew Kreisberg创作的电视连续剧。它基于DC漫画角色绿箭侠，一个由Mort Weisinger和George Papp创作的装扮奇特的犯罪打击战士。它于2012年10月10日在北美首映，与2012年末开始全球播出。主要拍摄于Vancouver、British Columbia、Canada，该系列讲述了亿万花花公子Oliver Queen，由Stephen Amell扮演，被困在敌人的岛屿上五年之后，回到家乡打击犯罪和腐败，成为一名武器是弓箭的神秘义务警员。不像漫画书中，Queen最初没有使用化名”绿箭侠“。
       </p>
       <p>
        由于这两个节目并列为我最喜爱的电视节目头衔，我一直想知道哪个节目更受其他人欢迎——谁会最终赢得这场收视率之战。 所以让我们写一个程序来预测哪个电视节目会有更多观众。 我们需要一个数据集，给出每一集的观众。幸运地，我从维基百科上得到了这个数据，并整理成一个.csv文件。它如下所示。
       </p>
       <table>
        <thead>
         <tr>
          <th>
           闪电侠
          </th>
          <th>
           闪电侠美国观众数
          </th>
          <th>
           绿箭侠
          </th>
          <th>
           绿箭侠美国观众数
          </th>
         </tr>
        </thead>
        <tbody>
         <tr>
          <td>
           1
          </td>
          <td>
           4.83
          </td>
          <td>
           1
          </td>
          <td>
           2.84
          </td>
         </tr>
         <tr>
          <td>
           2
          </td>
          <td>
           4.27
          </td>
          <td>
           2
          </td>
          <td>
           2.32
          </td>
         </tr>
         <tr>
          <td>
           3
          </td>
          <td>
           3.59
          </td>
          <td>
           3
          </td>
          <td>
           2.55
          </td>
         </tr>
         <tr>
          <td>
           4
          </td>
          <td>
           3.53
          </td>
          <td>
           4
          </td>
          <td>
           2.49
          </td>
         </tr>
         <tr>
          <td>
           5
          </td>
          <td>
           3.46
          </td>
          <td>
           5
          </td>
          <td>
           2.73
          </td>
         </tr>
         <tr>
          <td>
           6
          </td>
          <td>
           3.73
          </td>
          <td>
           6
          </td>
          <td>
           2.6
          </td>
         </tr>
         <tr>
          <td>
           7
          </td>
          <td>
           3.47
          </td>
          <td>
           7
          </td>
          <td>
           2.64
          </td>
         </tr>
         <tr>
          <td>
           8
          </td>
          <td>
           4.34
          </td>
          <td>
           8
          </td>
          <td>
           3.92
          </td>
         </tr>
         <tr>
          <td>
           9
          </td>
          <td>
           4.66
          </td>
          <td>
           9
          </td>
          <td>
           3.06
          </td>
         </tr>
        </tbody>
       </table>
       <p>
        观众数以百万为单位。
       </p>
       <p>
        <strong>
         解决问题的步骤：
        </strong>
       </p>
       <p>
        首先我们需要把数据转换为X_parameters和Y_parameters，不过这里我们有两个X_parameters和Y_parameters。因此，把他们命名为flash_x_parameter、flash_y_parameter、arrow_x_parameter、arrow_y_parameter吧。然后我们需要把数据拟合为两个不同的线性回归模型——先是闪电侠，然后是绿箭侠。 接着我们需要预测两个电视节目下一集的观众数量。 然后我们可以比较结果，推测哪个节目会有更多观众。
       </p>
       <p>
        <strong>
         步骤1
        </strong>
       </p>
       <p>
        导入我们的程序包：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_967807">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                # Required Packages
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                csv
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                matplotlib.pyplot as plt
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                numpy as np
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                pandas as pd
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python keyword">
                from
               </code>
               <code class="python plain">
                sklearn
               </code>
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                datasets, linear_model
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        <strong>
         步骤2
        </strong>
       </p>
       <p>
        写一个函数，把我们的数据集作为输入，返回flash_x_parameter、flash_y_parameter、arrow_x_parameter、arrow_y_parameter values。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_134234">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                # Function to get data
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                get_data(file_name):
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                data
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                pd.read_csv(file_name)
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                flash_x_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                flash_y_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                arrow_x_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                arrow_y_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                for
               </code>
               <code class="python plain">
                x1,y1,x2,y2
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python functions">
                zip
               </code>
               <code class="python plain">
                (data[
               </code>
               <code class="python string">
                'flash_episode_number'
               </code>
               <code class="python plain">
                ],data[
               </code>
               <code class="python string">
                'flash_us_viewers'
               </code>
               <code class="python plain">
                ],data[
               </code>
               <code class="python string">
                'arrow_episode_number'
               </code>
               <code class="python plain">
                ],data[
               </code>
               <code class="python string">
                'arrow_us_viewers'
               </code>
               <code class="python plain">
                ]):
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                flash_x_parameter.append([
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (x1)])
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                flash_y_parameter.append(
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (y1))
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                arrow_x_parameter.append([
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (x2)])
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                arrow_y_parameter.append(
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (y2))
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                flash_x_parameter,flash_y_parameter,arrow_x_parameter,arrow_y_parameter
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        现在我们有了我们的参数，来写一个函数，用上面这些参数作为输入，给出一个输出，预测哪个节目会有更多观众。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_967999">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                # Function to know which Tv show will have more viewers
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                more_viewers(x1,y1,x2,y2):
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr1
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                linear_model.LinearRegression()
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr1.fit(x1, y1)
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predicted_value1
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                regr1.predict(
               </code>
               <code class="python value">
                9
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python plain">
                predicted_value1
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                linear_model.LinearRegression()
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr2.fit(x2, y2)
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predicted_value2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                regr2.predict(
               </code>
               <code class="python value">
                9
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #print predicted_value1
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #print predicted_value2
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                if
               </code>
               <code class="python plain">
                predicted_value1 &gt; predicted_value2:
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                print
               </code>
               <code class="python string">
                "The Flash Tv Show will have more viewers for next week"
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                else
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number15 index14 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                "Arrow Tv Show will have more viewers for next week"
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        把所有东西写在一个文件中。打开你的编辑器，把它命名为prediction.py，复制下面的代码到prediction.py中。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_966496">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                # Required Packages
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                csv
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                matplotlib.pyplot as plt
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                numpy as np
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                pandas as pd
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python keyword">
                from
               </code>
               <code class="python plain">
                sklearn
               </code>
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                datasets, linear_model
               </code>
              </div>
              <div class="line number8 index7 alt1">
              </div>
              <div class="line number9 index8 alt2">
               <code class="python comments">
                # Function to get data
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                get_data(file_name):
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                data
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                pd.read_csv(file_name)
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                flash_x_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                flash_y_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                arrow_x_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number15 index14 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                arrow_y_parameter
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                for
               </code>
               <code class="python plain">
                x1,y1,x2,y2
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python functions">
                zip
               </code>
               <code class="python plain">
                (data[
               </code>
               <code class="python string">
                'flash_episode_number'
               </code>
               <code class="python plain">
                ],data[
               </code>
               <code class="python string">
                'flash_us_viewers'
               </code>
               <code class="python plain">
                ],data[
               </code>
               <code class="python string">
                'arrow_episode_number'
               </code>
               <code class="python plain">
                ],data[
               </code>
               <code class="python string">
                'arrow_us_viewers'
               </code>
               <code class="python plain">
                ]):
               </code>
              </div>
              <div class="line number17 index16 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                flash_x_parameter.append([
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (x1)])
               </code>
              </div>
              <div class="line number18 index17 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                flash_y_parameter.append(
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (y1))
               </code>
              </div>
              <div class="line number19 index18 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                arrow_x_parameter.append([
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (x2)])
               </code>
              </div>
              <div class="line number20 index19 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                arrow_y_parameter.append(
               </code>
               <code class="python functions">
                float
               </code>
               <code class="python plain">
                (y2))
               </code>
              </div>
              <div class="line number21 index20 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                flash_x_parameter,flash_y_parameter,arrow_x_parameter,arrow_y_parameter
               </code>
              </div>
              <div class="line number22 index21 alt1">
              </div>
              <div class="line number23 index22 alt2">
               <code class="python comments">
                # Function to know which Tv show will have more viewers
               </code>
              </div>
              <div class="line number24 index23 alt1">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                more_viewers(x1,y1,x2,y2):
               </code>
              </div>
              <div class="line number25 index24 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr1
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                linear_model.LinearRegression()
               </code>
              </div>
              <div class="line number26 index25 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr1.fit(x1, y1)
               </code>
              </div>
              <div class="line number27 index26 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predicted_value1
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                regr1.predict(
               </code>
               <code class="python value">
                9
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number28 index27 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python plain">
                predicted_value1
               </code>
              </div>
              <div class="line number29 index28 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                linear_model.LinearRegression()
               </code>
              </div>
              <div class="line number30 index29 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                regr2.fit(x2, y2)
               </code>
              </div>
              <div class="line number31 index30 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                predicted_value2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                regr2.predict(
               </code>
               <code class="python value">
                9
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number32 index31 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #print predicted_value1
               </code>
              </div>
              <div class="line number33 index32 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #print predicted_value2
               </code>
              </div>
              <div class="line number34 index33 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                if
               </code>
               <code class="python plain">
                predicted_value1 &gt; predicted_value2:
               </code>
              </div>
              <div class="line number35 index34 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                "The Flash Tv Show will have more viewers for next week"
               </code>
              </div>
              <div class="line number36 index35 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                else
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number37 index36 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                "Arrow Tv Show will have more viewers for next week"
               </code>
              </div>
              <div class="line number38 index37 alt1">
              </div>
              <div class="line number39 index38 alt2">
               <code class="python plain">
                x1,y1,x2,y2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                get_data(
               </code>
               <code class="python string">
                'input_data.csv'
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number40 index39 alt1">
               <code class="python comments">
                #print x1,y1,x2,y2
               </code>
              </div>
              <div class="line number41 index40 alt2">
               <code class="python plain">
                more_viewers(x1,y1,x2,y2)
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        可能你能猜出哪个节目会有更多观众——但运行一下这个程序看看你猜的对不对。
       </p>
       <h2>
        3) 替换数据集中的缺失值
       </h2>
       <p>
        有时候，我们会遇到需要分析包含有缺失值的数据的情况。有些人会把这些缺失值舍去，接着分析；有些人会用最大值、最小值或平均值替换他们。平均值是三者中最好的，但可以用线性回归来有效地替换那些缺失值。
       </p>
       <p>
        这种方法差不多像这样进行。
       </p>
       <p>
        首先我们找到我们要替换那一列里的缺失值，并找出缺失值依赖于其他列的哪些数据。把缺失值那一列作为Y_parameters，把缺失值更依赖的那些列作为X_parameters，并把这些数据拟合为线性回归模型。现在就可以用缺失值更依赖的那些列预测缺失的那一列。
       </p>
       <p>
        一旦这个过程完成了，我们就得到了没有任何缺失值的数据，供我们自由地分析数据。
       </p>
       <p>
        为了练习，我会把这个问题留给你，所以请从网上获取一些缺失值数据，解决这个问题。一旦你完成了请留下你的评论。我很想看看你的结果。
       </p>
       <p>
        <strong>
         个人小笔记：
        </strong>
       </p>
       <p>
        我想分享我个人的数据挖掘经历。记得在我的数据挖掘引论课程上，教师开始很慢，解释了一些数据挖掘可以应用的领域以及一些基本概念。然后突然地，难度迅速上升。这令我的一些同学感到非常沮丧，被这个课程吓到，终于扼杀了他们对数据挖掘的兴趣。所以我想避免在我的博客文章中这样做。我想让事情更轻松随意。因此我尝试用有趣的例子，来使读者更舒服地学习，而不是感到无聊或被吓到。
       </p>
       <p>
       </p>
       <p>
        文章出处：http://python.jobbole.com/81215/
       </p>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/20820.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/1-300x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20820.html" rel="bookmark" title="人们对Python在企业级开发中的10大误解">
         人们对Python在企业级开发中的10大误解
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20587.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/08/t0192a35f52bc6e5eab-300x188.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20587.html" rel="bookmark" title="基于Python的卷积神经网络和特征提取">
         基于Python的卷积神经网络和特征提取
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20577.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/08/t01393a74373db553ec-300x206.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20577.html" rel="bookmark" title="八大工具，透析Python数据生态圈最新趋势！">
         八大工具，透析Python数据生态圈最新趋势！
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20544.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/08/t01f6b96e9cd9bae4d9_副本1-300x178.png"/>
        </a>
        <a class="link" href="http://dataunion.org/20544.html" rel="bookmark" title="python有哪些好的学习资料或者博客？">
         python有哪些好的学习资料或者博客？
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F13355.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/13355.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/13355.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>